#! /bin/sh

source /etc/rc.d/common

SBD_CONFIG="/etc/sysconfig/sbd"
SBD="/usr/sbin/sbd"
SBD_LOCK="/var/lock/sbd"

check_args ${@}

# Make sure the SBD configuration file exists
if [ ! -f "${SBD_CONFIG}" ]; then
    /bin/echo "No SBD configuration found!"
    exit 0
fi

# Load configuration (and export all variables)
set -a
. ${SBD_CONFIG}
set +a

start() {
    /bin/echo "Starting sbd..."
    /bin/sh -c "/usr/bin/nohup ${SBD} ${SBD_OPTS} \
        -p /var/run/sbd.pid watch > /dev/null 2>&1 &"
    /bin/touch ${SBD_LOCK}
    /bin/sleep 1
}

stop() {
    /bin/echo "Stopping sbd..."
    /bin/kill -TERM $(/bin/pidof ${SBD}) || exit 1
    wait_for_stop ${SBD} && /bin/rm -f ${SBD_LOCK}
}

status() {
    /bin/pidof ${SBD} > /dev/null 2>&1
    exit ${?}
}

# Perform specified action
${1}
